{-------------------------------------------------------------
#
#	Apple Macintosh Developer Technical Support
#
#	MacApp Color QuickDraw Fractal Printing Sample Application
#
#	FracApp300
#
#	UFracApp300.p	-	Pascal Source
#
#	Copyright © 1988 Apple Computer, Inc.
#	All rights reserved.
#
#	Versions:	1.0					8/88
#
#	Components:	MFracApp300.p		August 1, 1988
#				UFracApp300.p		August 1, 1988
#				UFracApp300.inc1.p	August 1, 1988
#				FracApp300.r		August 1, 1988
#				FracApp300.make		August 1, 1988
#
#	The FracApp300 program is a version of the FracApp program that is
#	set up to be as compatible as possible.  It uses the PrGeneral call
#	in order to print at high resolution.  It demonstrates how to create
#	and save 300 dpi PICT files.  It uses an offscreen port to calculate
#	the data, and CopyBits to update the window on the screen.  When the
#	documents are written or read, the QuickDraw bottlenecks are used to
#	avoid having a huge memory hit during saving or opening. (800K of RAM
#	not needed, on a 640x480 screen, a big win.)  The Palette Manager is
#	used very slightly, only to associate a small palette with only Black
#	and White with each window.  This avoids needing the system palette
#	for each window, when no colors are used.  Since we are printing to
#	normal printers, we only use B&W in this version.
#			Written in MacApp Object Pascal code.
#			Compatibility rating = 1.  (The use of PrGeneral is slightly 
#				out of the ordinary, although supported.)
#
#	The program is a complete Macintosh application written in Object 
#	Pascal using MacApp.  It supports multiple windows, calculations in the
#	background under MultiFinder, use of the Palette Manager, reading and
#	writing of PICT files using the bottlenecks, and shows how to calculate
#	the Mandelbrot set.
#
#	There is a resource file that is necessary as well, to define the Menus, Window,
#	Dialog, and Palette resources used in the program.  
-------------------------------------------------------------}
{Copyright 1988 by Bob.  All rights reserved, since Bob has all rights.
	February 1, 1988.
	Written by Bo3b Johnson of Developer Technical Support. }

UNIT UFracApp300;

(*

This is a not too small application which can calculate a fractal in full color
	on the Mac II, using direct 881 code for speed.  It saves files on disk as PICT.
	For full info, see the implementation.

*)

INTERFACE

USES
	{$LOAD FracApp881.LOAD}
		MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
		UObject, UList, UMacApp,
		PaletteMgr, UPrinting,
	{$LOAD}
		MacPrint;

CONST

  {Command numbers}

	kSignature					= 'Arf3';		{Application signature}
	kFileType					= 'PICT';		{File-type code used for document files
																created by this application}
	kFracAppWindowID	= 1001;		{This is passed to NewSimpleWindow as the
															resource ID of the the WIND Resource
															which defines the window for this
															application's documents}
	kStaggerAmount			= 30;
	kPICTHeaderSize 		= 512;		{ 512 bytes off the file are used for our info and print info. }
	kSelPattern					= 128;		{ pattern resource Id. }
	kNewFractal 				= 1000;		{ item Id for New Fractal menu option. }
	kWrongMachine			= 1000;		{ error code if we cannot run, from ForceEnvirons. }
	kBadPrinter				= 1001;		{ error code if printer doesn't support PrGeneral. }
	kRectRight					= 2240;		{ Full page size rectangle right edge at 300 dpi }
	kRectBottom				= 3000;		{ Full page size for bottom at 300 dpi }
	
	envDontCare 				= 0;				{ don't care is always 0, for ForceEnvirons. }

TYPE
	{ The header record for each of the files saved by FracApp.  This header includes the
		pertinent data that allows a fractal to be restarted, as well as displayed
		on the screen.  The print record follows this info in the first 512 bytes of a file,
		but that info is read by the standard DoRead/DoWrite methods.  Some of the fields
		are LongInts to avoid rounding errors during calculations. }
	FracRecord  = RECORD
		fType:		OSType;			{ 4  set as 'Arf3' for these documents. }
		hdrId:		Integer;			{ 2  as 'FA' FracApp header ID. }
		version:	Integer;  			{ 2  file version decides type of file. }
		done:		Boolean;			{ 2  if the fractal is finished or not. }
		realMin:	Extended;			{ 12  minimum value of fractal on real/p axis. }
		realMax:	Extended;			{ 12  maximum value of fractal on real/p axis. }
		imagMin:	Extended;			{ 12  minimum on imaginary/q axis. }
		imagMax:	Extended;			{ 12  maximum on imaginary/q axis. }
		deltaP:		Extended;			{ 12  horizontal step size in fractal space. }
		deltaQ:		Extended;			{ 12  vertical step size in fractal space. }
		plotWidth: LongInt;			{ 4  width of fractal area in pixels. }
		plotHeight: LongInt;			{ 4  height of fractal area in pixels. }
		calcRect:	Rect;				{ 8  rectangle surrounding the window it was built for. }
		curRow:	LongInt;			{ 4  counter for current pixel position to be done.  Vertical. }
		curCol:		LongInt;			{ 4  counter for current pixel.  Horizontal. }
		elapsedTime: LongInt;		{ 4  amount of time to do this fractal in seconds. }
	END;  { FracRecord. }



{-------------------------------  Application  -------------------------------}

	TFracAppApplication = OBJECT(TApplication)


		PROCEDURE TFracAppApplication.IFracAppApplication (itsMainFileType: OSType);
			{Initializes the application and globals.}

		FUNCTION  TFracAppApplication.DoMakeDocument(
				itsCmdNumber: CmdNumber): TDocument; OVERRIDE;
			{Launches a TFracAppDocument; called when application's icon is
				opened, and when New or Open is requested by the user.
				Every application which uses Documents MUST override this
				method}

		PROCEDURE  TFracAppApplication.DoIdle (Phase: IdlePhase); OVERRIDE;
			{ Performs Idle time processing for the application.  This will do the
				fractal calculation during the idle times.  It will allow each open
				view a chance to calculate. }

	END;	{ TFracAppApplication }


{-------------------------------  Document  -------------------------------}

	TFracAppDocument = OBJECT(TDocument)

		{ Now the fields that are special to the fractal itself.  These are the
			variables that make up the display state and the definition of the
			fractal itself.  They are associated with the document so they can
			be stored in a file, and read back in.  Essentially a global state for
			each fractal document. }

		fFracHeader:						FracRecord;		{ global state on fractal. }
		fStartTime:							LongInt;				{ starting time of calculations. }
		fBigBuff:								Ptr;						{ pointer to offscreen data }
		fDrawingPort:						GrafPtr;				{ pointer to drawing buffer. }

		fFracAppView:						TFracAppView;
			{Every document object must preserve references to all the views it
				creates as fields of the document object, since DoMakeWindows
				will need to know which views to install in the windows it
				creates}


		PROCEDURE  TFracAppDocument.BuildOffWorld (sizeOfDoc: Rect);
			{ Allocates offscreen gDevice and port for document data. }

		PROCEDURE  TFracAppDocument.SetUpConstants;
			{ Sets up starting constants for calculation. }

		PROCEDURE  	TFracAppDocument.IFracAppDocument;
			{Init routine for the document, sets up the object, then the fractal default state. }

		PROCEDURE TFracAppDocument.DoInitialState;  OVERRIDE;
			{ Does the work for a New operation, where we start with a new fractal
				that doesn't have any stored data.  This is set up the view with no
				data and set up the fractal coordinates to the default. }

		PROCEDURE TFracAppDocument.DoMakeWindows; OVERRIDE;
			{Launches the window which will represent the document on the
				screen.  Every document which has any screen display MUST
				override this method}

		PROCEDURE TFracAppDocument.DoMakeViews(forPrinting: BOOLEAN); OVERRIDE;
			{Launches the view which is seen in the document's window.  Every
				document which has any screen display or which can be printed
				MUST override this method}

		PROCEDURE TFracAppDocument.DoNeedDiskSpace(VAR dataForkBytes,
												rsrcForkBytes: LONGINT);  OVERRIDE;
			{ Finds out the entire size of the object to be saved into the file so that the
				correct amount of disk space can be used.  }

		PROCEDURE TFracAppDocument.DoRead(aRefNum: INTEGER; rsrcExists,
												forPrinting: BOOLEAN);	OVERRIDE;
			{ Reads in the data out of the data fork of the file, and stows it into the
				documents bitMap.  Also resets the vars in the document object to match
				the saved values from the header. }

		PROCEDURE TFracAppDocument.DoWrite(aRefNum: INTEGER;
												makingCopy: BOOLEAN);	OVERRIDE;
			{ Converts the data in the documents port into a PICT, then writes that block
				out to the file, making it into a standard PICT file. }

		PROCEDURE TFracAppDocument.FreeData; OVERRIDE;
			{}

		PROCEDURE TFracAppDocument.Free; OVERRIDE;
			{ Free method for our document.  It disposes the data block of the picture
				data that was read in from the disk, and kills offscreen stuff. }

		PROCEDURE  TFracAppDocument.CalcCity;
			{ Does the idle time calculations for the document.  This is for the actual
				fractal calculations.  It is called by the handler for the Application
				DoIdle.  It is called for all open documents. }

	END;	{ TFracAppDocument }



{-------------------------------  View  -------------------------------}

	TFracAppView = OBJECT(TView)

		{ These fields are for the use of the view, and help define our specific view, that
			is the offscreen bitMap representation of the fractal. }
		fSelectionRect:					Rect;		{ rectangle that is current selection }
		fFracAppDocument:				TFracAppDocument;	{ handy to avoid type coercion. }

		PROCEDURE 	TFracAppView.IFracAppView (itsDocument: TFracAppDocument;
				sizeOfView: Rect);
			{ Inits the view object itself. }

		PROCEDURE TFracAppView.Draw(area: Rect); OVERRIDE;
			{Draws the view seen in the window.  Every nonblank view MUST
				override this method}

		FUNCTION TFracAppView.DoMenuCommand(
												aCmdNumber: CmdNumber): TCommand;  OVERRIDE;
			{ Handle the menu choices for New Fractal out of the Fractal Menu. }

		PROCEDURE TFracAppView.DoSetupMenus;  OVERRIDE;
			{ Set up the New Fractal menus choice in Fractal Menu, based on selection. }

		FUNCTION  TFracAppView.DoMouseCommand(VAR downLocalPoint: Point;
						VAR info: EventInfo;
						VAR hysteresis: Point): TCommand;	OVERRIDE;
			{ Handle the mouse events in the view.  Needs to do the selection of a new
				range for the next fractal to be calculated.  }

		PROCEDURE TFracAppView.DoHighlightSelection(fromHL, toHL: HLState); OVERRIDE;
			{ Highlight the current selection rectangle if there is one.  }

	END;	{ TFracAppView }


{-------------------------------  Command  -------------------------------}

	TAreaSelector = OBJECT(TCommand)

		{ These fields are for the command objects that we use.  The first one we need
			for sure is the selection object to handle the mouse selection in the content
			region of the view.  }
		fOwnerView:	TFracAppView;


		PROCEDURE TAreaSelector.IAreaSelector(ownerView: TFracAppView; startPt: Point);
			{ Initialize the selection object itself.  This basically sets up with IObject,
				then sets the fSelectionRect to be a minimal rectangle. }

		FUNCTION  TAreaSelector.TrackMouse(aTrackPhase: TrackPhase;
						VAR anchorPoint, previousPoint, nextPoint: Point;
						mouseDidMove: BOOLEAN): TCommand;  OVERRIDE;
			{ Track the mouse while the button is down in the view. }

		PROCEDURE TAreaSelector.TrackFeedback(anchorPoint, nextPoint: Point;
										turnItOn, mouseDidMove: BOOLEAN);  OVERRIDE;
			{ Feedback that matches better, using the selection pattern. }

		PROCEDURE TAreaSelector.TrackConstrain(anchorPoint, previousPoint: Point;
						VAR nextPoint: Point);  OVERRIDE;
			{ Constrain the mouse movement to be a rect which matches the screen. }

	END;	{ TAreaSelector }

{-------------------------------  TFracAppPrintHandler  --------------------------}

{ A print handler for doing 300 dpi printing, bypassing the print manager for most of
	it.  Uses the port created for calculating the fractal, and prints it using PostScript
	commands.  It is a print handler object so we get the fields and methods of the
	printing object.  Since it is an event handler, it will handle the menu events for
	the print operation, as well as enable their use. }

	TFracAppPrintHandler = OBJECT (TPrintHandler)
	
		fOwningView:	TFracAppView;		{ so we can avoid type coercion. }

		
		PROCEDURE 	TFracAppPrintHandler.IFracAppPrintHandler (itsView: TFracAppView);
			{ Inits the print handler object itself. }

		FUNCTION TFracAppPrintHandler.Print(itsCmdNumber: CmdNumber; 
							VAR proceed: BOOLEAN): TCommand; OVERRIDE;
			{ Override the print method so we can pass off the Finder printing to DoPrintingCommand }
			
		PROCEDURE TFracAppPrintHandler.DoSetupPrintingMenus;  OVERRIDE;
			{ Set up the New Fractal menus choice in Fractal Menu, based on selection. }

		FUNCTION  TFracAppPrintHandler.DoPrintingCommand(
												aCmdNumber: CmdNumber): TCommand;  OVERRIDE;
			{ Handle the menu choices for New Fractal out of the Fractal Menu. }

	END;	{ TFracAppPrintHandler }

IMPLEMENTATION

{$I UFracApp300.inc1.p}

END.
